package cn.wostore.wxhelper.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import cn.wostore.wxhelper.utils.DaoHandler;
import cn.wostore.wxhelper.utils.MyDatabaseHelper;
import cn.wostore.wxhelper.utils.L;

/**
 * @author wuch
 * @date 2018/12/28 0028
 */
public class DbProvider extends ContentProvider {
    private static final String TAG = DbProvider.class.getSimpleName();

    private static final String AUTHORITY = "cn.wostore.wxhelper.provider";
    private MyDatabaseHelper mMyDatabaseHelper;

    private static UriMatcher mUriMatcher;

    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        mUriMatcher.addURI(AUTHORITY, "ForwardRule", 0);
    }

    private String getTableName(Uri uri) {
        int code = mUriMatcher.match(uri);
        switch (code) {
            case 0:
                return "ForwardRule";
            default:
                return null;
        }
    }

    @Override
    public boolean onCreate() {
        L.d(TAG, "onCreate()");
        mMyDatabaseHelper = new MyDatabaseHelper(getContext());
        return (mMyDatabaseHelper == null) ? false : true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        L.d(TAG, "开始查询");
        String table = getTableName(uri);
        L.d(TAG, table);
        return DaoHandler.getInstance().query(table, projection, selection, selectionArgs, sortOrder);
    }

    @Override
    public String getType(Uri uri) {
        return "vnd.android.cursor.dir/vnd." + AUTHORITY + "." + MyDatabaseHelper.DB_NAME;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        L.d(TAG, "开始插入");
        String table = getTableName(uri);
        DaoHandler.getInstance().insert(table, null, values);
        return uri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }
}
